

import asyncio
import json
import os

import dotenv
import requests
from h2o_wave import Q, app, ui, on, data, run_on, AsyncSite  # noqa F401

from firebase_tools.authenticate import authenticate_with_firebase, check_str_token_validity
from h2o_dashboard.pages.documentation_page import documentation_page
from h2o_dashboard.pages.okx_dashbaord_page.okx_dashboard_page import okx_dashboard_page
from h2o_dashboard.pages.overview_page import overview_page
from h2o_dashboard.util import add_card, clear_cards

dotenv.load_dotenv(dotenv.find_dotenv())

FIREBASE_CONFIG = {
    "apiKey": os.environ.get("FIREBASE_API_KEY"),
}
assert FIREBASE_CONFIG["apiKey"], "FIREBASE_API_KEY environment variable not set!"


async def init(q: Q) -> None:
    """
    Q Page Meta (meta_card) Arguments:
        box
            A string indicating how to place this component on the page.
        title
            The title of the page.
        refresh
            Refresh rate in seconds. A value of 0 turns off live-updates. Values != 0 are currently ignored (reserved for future use).
        notification
            Display a desktop notification.
        notification_bar
            Display an in-app notification bar.
        redirect
            Redirect the page to a new URL.
        icon
            Shortcut icon path. Preferably a .png file (.ico files may not work in mobile browsers). Not supported in Safari.
        layouts
            The layouts supported by this page.
        dialog
            Display a dialog on the page.
        side_panel
            Display a side panel on the page.
        theme
            Specify the name of the theme (color scheme) to use on this page. One of 'light', 'neon' or 'h2o-dark'.
        themes
            Themes (color schemes) that define color used in the app.
        tracker
            Configure a tracker for the page (for web analytics).
        scripts
            External Javascript files to load into the page.
        script
            Javascript code to execute on this page.
        stylesheet
            CSS stylesheet to be applied to this page.
        stylesheets
            External CSS files to load into the page.
        commands
            Contextual menu commands for this component.
    """
    # Static Business Website
    # index_file = open('static/html/index.html', 'r').read()

    q.page['meta'] = ui.meta_card(box='',
                                  title='AntBot',
                                  layouts=[ui.layout(breakpoint='xs', min_height='100vh', name='default',
                                                     zones=[
                                      ui.zone('main', size='1', direction=ui.ZoneDirection.ROW, zones=[
                                          ui.zone('sidebar', size='180px'),
                                          ui.zone('body', zones=[
                                              ui.zone('header'),
                                              ui.zone('content', zones=[
                                                  # Specify various zones and use the one that is currently needed. Empty zones are ignored.
                                                  ui.zone('first_context', size='0 0 1 4',
                                                          direction=ui.ZoneDirection.ROW,
                                                          zones=[
                                                              ui.zone('first_context_1', size='1 4 0 0'),
                                                              ui.zone('first_context_2', size='1 4 0 0'),
                                                              ui.zone('first_context_3', size='1 4 0 0'),
                                                              ui.zone('first_context_4', size='1 4 0 0'),
                                                          ]),
                                                  ui.zone('second_context', size='0 0 1 4',
                                                          direction=ui.ZoneDirection.ROW,
                                                          zones=[
                                                              ui.zone('second_context_1', size='1 4 0 0'),
                                                              ui.zone('second_context_2', size='1 4 0 0'),
                                                              ui.zone('second_context_3', size='1 4 0 0',
                                                                      direction=ui.ZoneDirection.ROW,
                                                                      zones=[
                                                                          ui.zone('second_context_3_1', size='1 4 0 0'),
                                                                          ui.zone('second_context_3_2', size='1 4 0 0')
                                                                      ]),
                                                          ]),
                                                  ui.zone('details', size='4 4 4 4'),
                                                  ui.zone('horizontal', size='1', direction=ui.ZoneDirection.ROW),
                                                  ui.zone('centered', size='1 1 1 1', align='center'),
                                                  ui.zone('vertical', size='1'),
                                                  # Wrapping = 'start', 'end', 'center', 'between', 'around', 'stretch'
                                                  # Layout = 'start', 'end', 'center', 'between', 'around'
                                                  ui.zone('grid_1', direction=ui.ZoneDirection.ROW, wrap='stretch',
                                                          justify='center'),
                                                  ui.zone('grid_2', direction=ui.ZoneDirection.ROW, wrap='stretch',
                                                          justify='start'),
                                                  ui.zone('grid_3', direction=ui.ZoneDirection.ROW, wrap='stretch',
                                                          justify='center'),
                                                  ui.zone('grid_4', direction=ui.ZoneDirection.ROW, wrap='stretch',
                                                          justify='center'),
                                                  ui.zone('grid_5', direction=ui.ZoneDirection.ROW, wrap='stretch',
                                                          justify='center'),
                                                  ui.zone('grid_6', direction=ui.ZoneDirection.ROW, wrap='stretch',
                                                          justify='center'),
                                                  ui.zone('bot_manual_controls',
                                                            direction=ui.ZoneDirection.ROW,
                                                          # Cover the entire card
                                                            wrap='stretch',

                                                            ),
                                              ]),
                                          ]),
                                      ]),
                                      ui.zone('footer', size='0 1 0 0', direction=ui.ZoneDirection.ROW),
                                  ]),
                                           ],
                                  tracker=ui.tracker(type=ui.TrackerType.GA, id='G-Q21Z3V0G9K'),
                                  themes=[
                                      ui.theme(
                                          name='my-awesome-theme',
                                          primary='#8C1B11',  # Header and Sidebaer - Color Light Red
                                          text='#000000',  #
                                          card='#ffffff',
                                          page='#F2F2F2',
                                          # page='#D91A1A',

                                      ),
                                      ui.theme(
                                          name='my-awesome-theme1',
                                          primary='#571305',  # Header and Sidebaer - Color Light Red
                                          text='#E0E0E0',  #
                                          card='#333333',
                                          page='#121212',

                                      ),
                                      ui.theme(
                                          name='my-awesome-theme2',
                                          primary='#2979FF',  # Header and Sidebaer - Color Light Red
                                          text='#E0E0E0',  #
                                          card='#212121',
                                          page='#000000',

                                      )
                                  ],
                                  theme='my-awesome-theme2'
                                  )

    q.client.initialized = False


async def initialize_client(q: Q):
    q.client.cards = set()
    await init(q)
    q.client.initialized = True
    q.client.token = None  # Initially, no token is set.





async def render_login_page(q: Q, error_message=None):
    """Render the login page."""

    await clear_cards(q)
    items = [
        ui.message_bar(type='info', text='Please login to continue.', ),
        ui.text_xl('Login'),
        ui.textbox(name='email', label='Email', required=True),
        ui.textbox(name='password', label='Password', required=True, password=True),
        ui.buttons([ui.button(name='login', label='Login', primary=True, )]),
    ]
    if error_message:
        items.insert(0, ui.message_bar(type='error', text=error_message, ))

    await add_card(q, 'login', ui.form_card(box='centered', items=items, ), )

    # Lets add decorations to the login page


async def add_application_sidebar(q):
    image_address = None
    # image_address = ''
    if not q.args["#"]:
        q.args['#'] = 'overview_page'

    await add_card(q, 'Application_Sidebar', ui.nav_card(
        box='sidebar', color='primary', title='AntBot',
        subtitle="just a tiny bot here to help you out",
        value=f'#{q.args["#"]}',
        # image='https://wave.h2o.ai/img/h2o-logo.svg', items=[]
        # Loading from local file rather than url
        image=image_address,
        items=[
            ui.nav_group('', items=[
                ui.nav_item(name='#overview_page', label='Overview', icon='Home'),
                ui.nav_item(name='#okx_dashboard_page', label='OKX Dashboard', icon='eDiscoveryApp'),
                ui.nav_item(name='#documentation_page', label='Documentation', icon='Document'),
            ]),
            # ui.nav_group('Docs', items=[
            #     ui.nav_item(name='#admin_userdocs_link', label='Usage Documentation', icon='TextDocumentShared'),
            #     ui.nav_item(name='#admin_devdocs_link', label='Codebase Documentation', icon='TextDocumentEdit'),
            # ]),
            # ui.nav_group('Account', items=[
            #     ui.nav_item(name='#admin_account_page', label='Account', icon='AccountManagement'),
            #     ui.nav_item(name='#admin_plans_page', label='Plans', icon='PaymentCard'),
            #     ui.nav_item(name='logout', label='Logout', icon='Logout'),
            # ]),

        ],
    ))



async def render_hidden_content(q: Q):
    """
    Render pages content e.g. overview_page or other pages get added here
    """
    await add_application_sidebar(q)
    await q.page.save()

    # What path is the user at right now
    print(f'Path: {q.args["#"]}')



    if q.args['#'] == 'overview_page':
        print("Route to Overview Page")
        q.client.okx_dashboard_page_running_event.clear()
        q.client.documentation_page_running_event.clear()
        if not q.client.overview_page_running_event.is_set():
            await clear_cards(q)
            await add_application_sidebar(q)
            await q.page.save()
            q.client.overview_page_running_event.set()
            q.client.overview_page_task = asyncio.create_task(overview_page(q,update_seconds=1))
        await q.page.save()

    elif q.args['#'] == 'okx_dashboard_page':
        print("Route to OKX Dashboard Page")
        q.client.overview_page_running_event.clear()
        q.client.documentation_page_running_event.clear()
        if not q.client.okx_dashboard_page_running_event.is_set():
            await clear_cards(q)
            await add_application_sidebar(q)
            await q.page.save()
            q.client.okx_dashboard_page_running_event.set()
            q.client.okx_dashboard_page_task = asyncio.create_task(okx_dashboard_page(q, update_seconds=1))
        await q.page.save()
    elif q.args['#'] == 'documentation_page':
        print("Route to Documentation Page")
        q.client.overview_page_running_event.clear()
        q.client.okx_dashboard_page_running_event.clear()
        if not q.client.documentation_page_running_event.is_set():
            await clear_cards(q)
            await add_application_sidebar(q)
            await q.page.save()
            q.client.documentation_page_running_event.set()
            q.client.documentation_page_task = asyncio.create_task(documentation_page(q))
        await q.page.save()
    else:
        print("Error: No route found")

    await q.page.save()


async def serve_security(q: Q, bypass_security=False):
    response = None
    # If logout is triggered, clear token and show login page
    if q.args.logout:
        q.client.token = None
        await render_login_page(q)
        return  # End the function after logging out

    # If login is triggered, try to authenticate and decide what to render next
    if q.args.login:
        response = await authenticate_with_firebase(q.args.email, q.args.password)

        if response['status'] == 'success':
            print("Token already exists and is valid")
            q.client.token = response['token']
            q.client.user_id = response['user_id']
            q.client.email = q.args.email
            q.client.expires_in = response['expires_in']
            q.client.password = q.args.password


            await render_login_page(q, error_message=response['error_message'])
        else:
            q.client.token = None
            q.client.user_id = None
            q.client.email = None
            q.client.expires_in = None
            q.client.password = None
            await render_login_page(q, error_message=response['error_message'])
            return  # End the function if login fails

    if bypass_security:
        q.client.token = 'bypass'
        await render_hidden_content(q)
        return

    # If the client already has a token, check its validity and act accordingly
    if q.client.token:
        if await check_str_token_validity(q.client.token):
            context = response  # todo: implement better context handling
            await render_hidden_content(q)
        else:
            q.client.token = None
            await render_login_page(q)
    else:
        await render_login_page(q)
